package com.yilijk.base.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.annotation.NonNull;

public class SharedPreferencesUtils {
    private static SharedPreferencesUtils instance = new SharedPreferencesUtils();
    private static SharedPreferences mSp;
    private static SharedPreferences Ument_mSp;
    private static SharedPreferences NetWork_mSp;
    private static SharedPreferences pop_mSp;
    private static SharedPreferences loginTag_mSp;


    private Context mContext;

    private SharedPreferencesUtils() {
    }


    public static SharedPreferencesUtils getInstance() {

        return instance;
    }

//    初始化

    public static void initSpUtils(Context context) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("hytx_appInfo", Context.MODE_PRIVATE);
            pop_mSp = context.getSharedPreferences("hytx_appPop", Context.MODE_PRIVATE);
            Ument_mSp = context.getSharedPreferences("hytx_Ument", Context.MODE_PRIVATE);
            NetWork_mSp = context.getSharedPreferences("hytx_url", Context.MODE_PRIVATE);
            loginTag_mSp = context.getSharedPreferences("hytx_login_tag", Context.MODE_PRIVATE);

        }
    }

    //保存
    public void saveurl(String key, Object value) {
        saveCommon(NetWork_mSp, key, value);
    }

    public void save(String key, Object value) {
        saveCommon(mSp, key, value);
    }

    public void saveUment(String key, Object value) {
        saveCommon(Ument_mSp, key, value);
    }

    public void savePop(String key, Object value) {
        saveCommon(pop_mSp, key, value);
    }
    //保存
    public void saveLoginTag(String key, Object value) {
        saveCommon(loginTag_mSp,key,value);
    }

    /**
     * 加密存储
     *
     * @return
     */
    public void saveCommon(SharedPreferences mSp, String key, Object value) {
        if (value == null) {
            return;
        }
        mSp.edit().putString(key, Base64Utils.encode(value.toString().getBytes())).commit();
    }

    //取
    public String getPop_String(String key, String defValue) {
        return pop_mSp.getString(key, defValue);
    }

    public boolean getPop_Boolean(String key, boolean defValue) {
        String string = getAllData(pop_mSp, key, defValue + "");
        if ((string.length() > 0)) {
            return Boolean.valueOf(string);
        } else {
            return defValue;
        }
    }

    public int getPop_Integer(String key, int defValue) {
        Log.e("getPop_Integer", "getPop_Integer: " + key);
        String string = getAllData(pop_mSp, key, defValue + "");
        if ((string.length() > 0)) {
            return Integer.valueOf(string);
        } else {
            return defValue;
        }
    }

    //取
    public String geturl_String(String key, String defValue) {
        return getAllData(NetWork_mSp, key, defValue);
    }

    public String getUment_String(String key, String defValue) {
        return getAllData(Ument_mSp, key, defValue);
    }

    public boolean getLoginTagBoolean(String key, boolean defValue) {
        String type = getAllData(loginTag_mSp,key,defValue+"");
        return type!=null && type.length()>0?Boolean.valueOf(type):false;
    }


    //取
    public String getString(String key, String defValue) {
        return getAllData(mSp, key, defValue);
    }

    public boolean getBoolean(String key, boolean defValue) {
        String string = getAllData(mSp, key, defValue + "");
        if ((string.length() > 0)) {
            return Boolean.valueOf(string);
        } else {
            return defValue;
        }
    }

    //    public int getInteger(String key, int defValue) {
//        return mSp.getInt(key, defValue);
//    }
    public long getLong(String key, long defValue) {
        String string = getAllData(mSp, key, defValue + "");
        if ((string.length() > 0)) {
            return Long.valueOf(string);
        } else {
            return defValue;
        }
    }

    /**
     * 取数据解密
     *
     * @return
     */
    public String getAllData(SharedPreferences mSp, String key, String defValue) {
        Log.e("getPop_Integer", "getPop_Integer: " + key);
        String value = mSp.getString(key, defValue);
        if (value != defValue) {
            try {
                String s = new String(Base64Utils.decode(value));
                if ("null".equals(s)){
                    return defValue;
                }else{
                    return s;
                }
            }catch (Throwable t){
                return value;
            }
        } else {
            return defValue;
        }
    }

    public boolean removeData(@NonNull String[] keys) {
        for (int i = 0; i < keys.length; i++) {
            mSp.edit().remove(keys[i]);
        }
        mSp.edit().apply();
        return true;
    }

    @SuppressLint("CommitPrefEdits")
    public boolean removeAll(Context context) {
        SharedPreferences.Editor edit = mSp.edit();
        edit.clear();
        edit.commit();
        return true;
    }

    @SuppressLint("CommitPrefEdits")
    public boolean removePopAll(Context context) {
        SharedPreferences.Editor edit = pop_mSp.edit();
        edit.clear();
        edit.commit();
        return true;
    }
    @SuppressLint("CommitPrefEdits")
    public boolean removeAll(){
        SharedPreferences.Editor edit = mSp.edit();
        edit.clear();
        edit.commit();
        return true;
    }

    @SuppressLint("CommitPrefEdits")
    public boolean removeUmeng() {
        SharedPreferences.Editor edit = Ument_mSp.edit();
        edit.clear();
        edit.commit();
        return true;
    }

    @SuppressLint("CommitPrefEdits")
    public boolean removePop() {
        SharedPreferences.Editor edit = pop_mSp.edit();
        edit.clear();
        edit.commit();
        return true;
    }
}
